package cn.tsinghua.comsmacitccntsinghua.broadcast;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;

import android.os.Handler;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.Port.myserialport.DataUtils;
import cn.tsinghua.comsmacitccntsinghua.DeviceInfo;

import cn.tsinghua.comsmacitccntsinghua.VO.BinVO;
import cn.tsinghua.comsmacitccntsinghua.bean.BaseDataInfo;
import cn.tsinghua.comsmacitccntsinghua.bean.EventReportBean;
import cn.tsinghua.comsmacitccntsinghua.bean.HandlerBridge;
import cn.tsinghua.comsmacitccntsinghua.bean.WarnReport;
import cn.tsinghua.comsmacitccntsinghua.bean.WeightBean;
import cn.tsinghua.comsmacitccntsinghua.tinker.SampleApplicationLike;
import cn.tsinghua.comsmacitccntsinghua.ui.MainActivity;

public class BroadReceiver extends BroadcastReceiver {
    private  BaseDataInfo mBaseDataInfo;
    private Handler mHandler;

    //内部接口Message
    private ObjMessage objeMessage;

    Boolean canSendVL = true;
    Timer timer = new Timer();


    public BroadReceiver(BaseDataInfo baseDataInfo, Handler handler) {
        this.mBaseDataInfo = baseDataInfo;
        this.mHandler = handler;
    }

    public BroadReceiver() {

    }

    @Override
    public void onReceive(Context context, Intent intent) {

        String back = intent.getStringExtra("data");

        Log.i("intentDataMe",""+back);

        Log.e("board",back);


        if (back.equals("VL")){
            String QRCode = intent.getStringExtra("value");
            Log.d("ReceiverQR",QRCode);
            HashMap<String,List<String>> codeMap = new HashMap<>();
            List<String> QA = new ArrayList<>();
            QA.add(QRCode);
            codeMap.put("id",QA);
            //接过之后一秒内不过去
            if(canSendVL){
                objeMessage.getObject(codeMap);
                canSendVL = false;
                TimerTask timerTask  = new TimerTask() {
                    @Override
                    public void run() {
                        canSendVL = true;
                    }
                };
                timer.schedule(timerTask,1000);//一秒后送过去
            }
//            EventBus.getDefault().post(QRCode);
        }
        if(back.equals("200012")){   //返回投口总重
            String[] weightarray = intent.getExtras().getStringArray("array");
            for(int i = 0; i < weightarray.length ; i++){
                Log.i("00012", String.valueOf(weightarray[i]));
            }
        }else if(back.equals("200013")){//顶灯照明控制
            Log.i("light","success");
            String status = intent.getStringExtra("value");
            if(status.equals("0")){
                Log.i("light","dark");
            }else{
                Log.i("light","bright");
            }
        }else if(back.equals("200014")){  //投口状态获取
            /**重写**/
            List<String> mouthStatusList = intent.getExtras().getStringArrayList("array");
//            for(int i = 0; i < mouthStatusList.size() ; i++){
//                Log.i("00014", String.valueOf(mouthStatusList.get(i)));
//            }
            HashMap<String,List<String>> hashMap = new HashMap<>();
            hashMap.put("mouth",mouthStatusList);
            objeMessage.getObject(hashMap);
            /**
            String[] wA = intent.getExtras().getStringArray("array");
            for(int i = 0; i < wA.length ; i++){
                Log.i("00014", String.valueOf(wA[i]));
            }
            //MainActivity处理
            HashMap<String,String[]> hashMap = new HashMap<>();
            hashMap.put("mouth",wA);
            objeMessage.getObject(hashMap);
             **/
        }else if(back.equals("200015")){//查询溢满状态 已合并到警报内  废弃
            /**
             String[] wA = intent.getExtras().getStringArray("array");
             for(int i = 0; i < wA.length ; i++){
             Log.i("00015溢满状态 ", String.valueOf(wA[i]));
             }
             //MainActivity处理
             HashMap<String,String[]> hashMap = new HashMap<>();
             hashMap.put("full",wA);
             objeMessage.getObject(hashMap);**/
        }else if(back.equals("200016")){ //箱内温度获取
            //重写
            List<String> boxTemperatureList = intent.getExtras().getStringArrayList("array");
            for(int i = 0; i < boxTemperatureList.size() ; i++){
                Log.i("00016", String.valueOf(boxTemperatureList.get(i)));
            }
//            int[] wA = intent.getExtras().getIntArray("array");
//            for(int i = 0; i < wA.length ; i++){
//                Log.i("00016", String.valueOf(wA[i]));
//            }
        }else if(back.equals("200017")){  //用户登出
            Log.i("logut","用户登出");
            List<String> weightStringList = intent.getExtras().getStringArrayList("array");
            for(int i = 0; i < weightStringList.size() ; i++){
                Log.i("00017", String.valueOf(weightStringList.get(i)));
            }
            //用户登出 这里需要送一个weightBean过去
            WeightBean weightBean = new WeightBean();
            List<WeightBean.DetailBean> details = new ArrayList<>();
            for(int i = 0; i < weightStringList.size();i++){
                WeightBean.DetailBean detailBean = new WeightBean.DetailBean();
                detailBean.setQuality(Integer.parseInt(weightStringList.get(i))*100);
                detailBean.setInner_number(i);
                details.add(detailBean);
            }
            weightBean.setDetail(details);
            if(weightBean != null){
                objeMessage.getObject(weightBean);
            }
        }else if(back.equals("200018")){ //警报信息
            List<String> WarnList = intent.getExtras().getStringArrayList("array");
            for(int i = 0; i < WarnList.size() ; i++){
                Log.i("00018", String.valueOf(WarnList.get(i)));
            }
//            String[] wA = intent.getExtras().getStringArray("array");
//            for(int i = 0; i < wA.length ; i++){
//                Log.i("00018", String.valueOf(wA[i]));
//            }
            //MainActivity处理
            HashMap<String,List<String>> hashMap = new HashMap<>();
            hashMap.put("warn",WarnList);
//            EventReportBean eventReportBean = new EventReportBean();
            objeMessage.getObject(hashMap);
        }else if(back.equals("200019")){//电机异常
            List<String> mortorErrorList = intent.getExtras().getStringArrayList("array");
            for(int i = 0; i < mortorErrorList.size() ; i++){
                Log.i("00019", String.valueOf(mortorErrorList.get(i)));
            }
//            String[] wA = intent.getExtras().getStringArray("array");
//            for(int i = 0; i < wA.length ; i++){
//                Log.i("00019", String.valueOf(wA[i]));
//            }
            //MainActivity处理
            HashMap<String,List<String>> hashMap = new HashMap<>();
            hashMap.put("mortorerror",mortorErrorList);
//            EventReportBean eventReportBean = new EventReportBean();
            objeMessage.getObject(hashMap);
        }else if(back.equals("200020")){ //控制板Id
            String controllerId = intent.getStringExtra("value");
            int length = controllerId.length();
            DeviceInfo.setBoardId(controllerId.substring(0,length-4));
            Log.i("controllerId", controllerId.substring(0,length-4));
            SampleApplicationLike.saveData("boardId",controllerId.substring(0,length-4));
            //拿到控制板ID就去请求DeviceID
            String temp = "deviceId";
            objeMessage.getObject(temp);
        }else if(back.equals("200021")){ //除臭延长
            Log.i("delaysucc", "除臭延长时间成功");
        }else if(back.equals("200022")){ //推杆时间
            Log.i("timesuccess", "推杆时间修改成功");
        }else if(back.equals("200023")){//电流阈值
            Log.i("maxCurrent", "电流阈值修改成功");
        }else if(back.equals("200010")){ //用户登录
            Log.i("usrLogin", "用户登录");
            //这里登录成功需要回传回来boardId
        } else if(back.equals("200011")){ //投口延长
            Log.i("mouhdelay", "投口延长成功");
        }
        else if(back.equals("200024")){
            List<String> openDoorStatus = intent.getExtras().getStringArrayList("array");
            HashMap<String,List<String>> hashMap = new HashMap<>();
            hashMap.put("doorStatus",openDoorStatus);
            objeMessage.getObject(hashMap);
        }
    }


    public interface ObjMessage{
        public void getObject(Object o);
    }

    public void setObjeMessage(ObjMessage message){
        this.objeMessage = message;
    }

    public Boolean loginByVL(){
        return null;
    }



}
